package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 预约服务实现类
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量添加预约数据
     * @param orderSettingList
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        //判空操作
        if(orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                //遍历，确定当前时间是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());

                if(count > 0){
                    //存在，更新数据
                    orderSettingDao.editNumberByOrderDate(orderSetting);

                }else {
                    //不存在，执行新增操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 根据年月查询预约信息
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        return orderSettingDao.getOrderSettingByMonth(date);
    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断输入的日期是否设置过
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            //设置过，执行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //未设置，执行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
